"""
Hello Plugin - 示例插件
演示插件的基本结构和功能
"""

import sys
import os

# 添加核心模块路径
plugin_core_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
if plugin_core_path not in sys.path:
    sys.path.insert(0, plugin_core_path)

from plugin_framework.plugin_base import PluginBase
from typing import Any


class HelloPlugin(PluginBase):
    """Hello插件示例"""
    
    def __init__(self) -> None:
        super().__init__()
        self._count = 0
    
    def on_load(self, context: Any) -> None:
        """插件加载时调用"""
        self.set_context(context)
        
        # 获取日志记录器
        logger = context.get_logger(self.name)
        logger.info(f"{self.name} loaded successfully!")
        
        # 读取配置
        greeting = context.get_config('greeting', 'Hello')
        language = context.get_config('language', 'en')
        logger.info(f"Config - greeting: {greeting}, language: {language}")
        
        # 注册到服务
        context.register_service('hello_service', self)
    
    def on_unload(self) -> None:
        """插件卸载时调用"""
        ctx = self.get_context()
        if ctx:
            logger = ctx.get_logger(self.name)
            logger.info(f"{self.name} unloaded. Total calls: {self._count}")
            
            # 注销服务
            ctx.unregister_service('hello_service')
    
    def run(self, *args: Any, **kwargs: Any) -> str:
        """主要功能入口"""
        self._count += 1
        
        ctx = self.get_context()
        greeting = ctx.get_config('greeting', 'Hello') if ctx else 'Hello'
        
        name = kwargs.get('name', 'World')
        message = f"{greeting}, {name}! (Call #{self._count})"
        
        if ctx:
            logger = ctx.get_logger(self.name)
            logger.info(message)
        
        return message
    
    def get_call_count(self) -> int:
        """获取调用次数"""
        return self._count